package com.wshl.aijiuunion.wxapi;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.wshl.aijiuunion.GlobConstant;
import com.wshl.aijiuunion.view.bean.EventBeans;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by LST on 2017/11/10.
 */

public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
    private static final String TAG = "WXEntryActivity";
    private static final int RETURN_MSG_TYPE_LOGIN = 1; //登录
    private static final int RETURN_MSG_TYPE_SHARE = 2; //分享


    private IWXAPI api;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        //注册微信appid
        api = WXAPIFactory.createWXAPI(this, null);
        api.registerApp(Constancts.WXAPPID);
        //如果没回调onResp，估计是这句没有写
        api.handleIntent(getIntent(), this);
    }


    // 微信发送请求到第三方应用时，会回调到该方法
    @Override
    public void onReq(BaseReq baseReq) {

    }

    // 第三方应用发送到微信的请求处理后的响应结果，会回调到该方法
    //app发送消息给微信，处理返回消息的回调
    @Override
    public void onResp(BaseResp baseResp) {
        Log.i(TAG, "onResp:------>");
        Log.i(TAG, "error_code:---->" + baseResp.errCode);
        int type = baseResp.getType(); //类型：分享还是登录
        switch (baseResp.errCode) {
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                //用户拒绝授权
                ToastUtils.showLong("拒绝授权微信登录");
                WXEntryActivity.this.finish();
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                //用户取消
                String message = "";
                if (type == RETURN_MSG_TYPE_LOGIN) {
                    message = "取消了微信登录";
                    ToastUtils.showLong("取消微信登录");
                    WXEntryActivity.this.finish();
                } else if (type == RETURN_MSG_TYPE_SHARE) {
                    message = "取消了微信分享";
                }
                ToastUtils.showLong(message);
                break;
            case BaseResp.ErrCode.ERR_OK:
                //用户同意
                if (type == RETURN_MSG_TYPE_LOGIN) {
                    LogUtils.i("微信授权成功");
                    //用户换取access_token的code，仅在ErrCode为0时有效
                    String code = ((SendAuth.Resp) baseResp).code;
                    LogUtils.i("code:" + code);

                    //这里拿到了这个code，去做2次网络请求获取access_token和用户个人信息
                    // EventBus.getDefault().post(new EventBeans(code));
                    EventBus.getDefault().post(new EventBeans(GlobConstant.WXLOGINGETACCESSTOKEN, code));
                    WXEntryActivity.this.finish();

                } else if (type == RETURN_MSG_TYPE_SHARE) {
                   // ToastUtils.showLong("微信分享成功");
                    WXEntryActivity.this.finish();
                    EventBus.getDefault().post(new EventBeans(GlobConstant.WXPAYRESULT, GlobConstant.WXSHARESUCCESS));
                }

                break;
            default:
                WXEntryActivity.this.finish();
                break;
        }
    }

}


